package com.lzx.adx.report.support.strategy.v3.media;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.lzx.adx.report.annnotions.MediaType;
import com.lzx.adx.report.constant.MediaChannelConstant;
import com.lzx.adx.report.dto.CallTypeMappingDTO;
import com.lzx.adx.report.dto.GtdResult;
import com.lzx.adx.report.dto.OcpxAccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.TreeMap;

/**
 * 网易云音乐回调
 */
@Component
@Slf4j
@MediaType(MediaChannelConstant.WANGYI_CLOUD_MUSIC)
public class WangYiCloudMusicChannel extends AbstractChannel {
    public static final String baseUrl = "https://ad-effect.music.163.com/ad/action";

    @Override
    public ResponseEntity callback(ReportDTO reportDTO, OcpxAccountDTO accountDTO, String callType, String ts) {
        boolean base = base(reportDTO, accountDTO, callType, ts);
        if (!base) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        Map<String, CallTypeMappingDTO> callTypeMap = getCallTypeMap(accountDTO);
        CallTypeMappingDTO callTypeMappingDTO = callTypeMap.get(callType);
        String eventType = callTypeMappingDTO.getMediaReturnType();
        String clickId = URLEncoderDecoderTool.decodeURL(reportDTO.getRequestId());
        Map<String, Object> param = new TreeMap<>();
        String[] split = accountDTO.getMediaKey().split(",");
        log.info("上报给网易云音乐的秘钥source[{}],appkey[{}],secretkey[{}]", split[0], split[1], split[2]);
        param.put("platType", "0");
        param.put("source", Integer.parseInt(split[0]));
        param.put("event", eventType);
        param.put("clickid", clickId);
        param.put("appkey", split[1]);
        param.put("timestamp", Long.valueOf(System.currentTimeMillis()) / 1000);
        param.put("sign", getSign(split[2], param));
        // 拼接字符串
        StringBuilder urlStr = new StringBuilder();
        for (Map.Entry<String, Object> entry : param.entrySet()) {
            urlStr.append("&").append(entry.getKey()).append("=").append(entry.getValue());
        }
        urlStr.deleteCharAt(0);
        String resultUrl = baseUrl + "?" + urlStr;
        log.info("账号为{} 请求流水号{} 上报给网易云音乐的请求的参数为{}", reportDTO.getDspId(), reportDTO.getBizRequestId(), resultUrl);
        String result = HttpUtil.get(resultUrl);
        log.info("账号为{} 请求流水号{} 上报给网易云音乐转换响应的结果为{}", reportDTO.getDspId(), reportDTO.getBizRequestId(), result);
        accountDTO.setFinalURL(resultUrl);
        accountDTO.setBody(result);
        return ResponseEntity.ok(new GtdResult<>());
    }

    private static String getSign(String secretKey, Map<String, Object> param) {
        StringBuilder sign = new StringBuilder();
        for (Map.Entry<String, Object> entry : param.entrySet()) {
            sign.append(secretKey).append(entry.getKey()).append(entry.getValue());
        }
        sign.append(secretKey);
        return SecureUtil.md5(sign.toString()).toUpperCase();
    }


}
